   '  10 461 394 944 000 (  16!) ' . ǳ -   ,          (      ).
      ,          .       ( [ ]   ).

1.    

  text
 1   2   3   4
 5   6   7   8
 9  10  11  12
13  14  15  [ ]
 ,  .
     (      ).text
[ ]  1   2   3
 4   5   6   7
 8   9  10  11
12  13  14  15
 ,  .
   (   ,    ).text
 1   5   9  13
 2   6  10  14
 3   7  11  15
 4   8  12  [ ]
 ,  .
    ( 15  1,    ).text
15  14  13  12
11  10   9   8
 7   6   5   4
 3   2   1  [ ]
 ,  .

2.  

  (       ).text
 1   2   3   4
 8   7   6   5
 9  10  11  12
[ ] 15  14  13
 ,  .
     (  ,   ).text
 1   2   3   4
12  13  14   5
11  [ ] 15   6
10   9   8   7
 ,  .
     (     ).text
 1  12  11  10
 2  13  [ ]  9
 3  14  15   8
 4   5   6   7
 ,  .
 ĳ  (    ).text
 1   2   4   7
 3   5   8  11
 6   9  12  14
10  13  15  [ ]
 ,  .

3.    

  (  )  ,   80  (     '   ,    ).text
[ ] 15  14  13
12  11  10   9
 8   7   6   5
 4   3   2   1
 ,  .
   ( 30)       0,      ,         30.text
[ ] 15  14   1
11   4   5  10
 7   8   9   6
12   3   2  13
 ,  .
  ( / )       ,   ,    .text
 1   2   3   4
 6   5   8   7
 9  10  11  12
14  13  [ ] 15
 ,  .
   ,         (,    )?












,  .     ,     ,    '  (      14  15).
  10   ,  100%      1  15.           ( ,  )   .     [ ].

  ( )

 1
text
 5   1   2   4
 9   6   3   8
13  10   7  12
14  15  11  [ ]
 ,  .
 2
text
 2   6   3   4
 1   5   7   8
 9  10  12  15
13  14  11  [ ]
 ,  .
 3
text
 4   1   2   3
 8   5   6   7
12   9  10  11
[ ] 13  14  15
 ,  .

  ( )

 4
text
11   4  14   2
 8  15   1   9
 6  13   3  12
 5  10   7  [ ]
 ,  .
 5
text
 9  12   3  14
 2   7  15   5
11   1   8   4
13   6  10  [ ]
 ,  .
 6
text
14   9   4   7
11   2  15   1
 6  13   8  12
 3  10   5  [ ]
 ,  .
 7
text
 7  13   2  10
15   4   8   1
12   9   5  14
 3  11   6  [ ]
 ,  .

 ( )

 8
text
15  11   6   2
12   8   3  14
 9   5  13   1
 4   7  10  [ ]
 ,  .
 9
text
13   8  15   4
 3  11   6   9
14   1  10   2
 7  12   5  [ ]
 ,  .
 10 ( 80   ')
text
[ ] 15  14  13
12  11  10   9
 8   7   6   5
 4   3   2   1
 ,  .
   ,     :
 ,      ,     (, 14  15)    .     ,        (, 15  14),      .
    - (),    -     ,     ?














!  ,     Delphi (  - ,  Delphi 7   RAD Studio).
     1  15,      ( +   )     ,   .

   Delphi (Console Application)

  : File -> New -> Other -> Console Application      :
pascal
program Puzzle15Generator;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBoard = array[0..15] of Integer;

// ,    
function IsSolvable(const Board: TBoard): Boolean;
var
  i, j, Inversions, EmptyRowFromBottom: Integer;
begin
  Inversions := 0;
  EmptyRowFromBottom := 0;

  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
    begin
      //       ( 1  4)
      EmptyRowFromBottom := 4 - (i div 4);
      Continue;
    end;

    //    (     )
    for j := i + 1 to 15 do
    begin
      if (Board[j] <> 0) and (Board[i] > Board[j]) then
        Inc(Inversions);
    end;
  end;

  //  '   4x4:
  //        (2  4),     .
  //    (1  3)   .
  if (EmptyRowFromBottom mod 2) = 0 then
    Result := (Inversions mod 2) = 1
  else
    Result := (Inversions mod 2) = 0;
end;

//   ' 
procedure GenerateSolvableBoard(var Board: TBoard);
var
  i, RandIdx, Temp: Integer;
begin
  repeat
    //    (0   )
    for i := 0 to 14 do
      Board[i] := i + 1;
    Board[15] := 0;

    //   Գ-
    for i := 15 downto 1 do
    begin
      RandIdx := Random(i + 1);
      Temp := Board[i];
      Board[i] := Board[RandIdx];
      Board[RandIdx] := Temp;
    end;
  until IsSolvable(Board); // ,    ' 
end;

//    4x4  
procedure PrintBoard(const Board: TBoard);
var
  i: Integer;
begin
  Writeln('+----+----+----+----+');
  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
      Write('|    ')
    else
      Write(Format('| %2d ', [Board[i]]));

    if (i + 1) mod 4 = 0 then
    begin
      Writeln('|');
      Writeln('+----+----+----+----+');
    end;
  end;
end;

var
  Board: TBoard;
  UserChoice: string;
  Count: Integer;
begin
  Randomize; //    
  Count := 1;

  Writeln('===     "15" ===');
  Writeln('   100% .');
  Writeln;

  repeat
    Writeln(' #', Count, ':');
    GenerateSolvableBoard(Board);
    PrintBoard(Board);
    Inc(Count);

    Writeln;
    Write(' [Enter]      "q"  : ');
    Readln(UserChoice);
    Writeln;
  until LowerCase(Trim(UserChoice)) = 'q';
end.
 ,  .

  :

  :  IsSolvable    (  )      (0).          ,   .
 :       '  ',  repeat ... until     (     ).
 :     4x4. ϳ       Enter,    ,   q,   .
         1020     (.txt),          ?
















program Puzzle15Generator;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBoard = array[0..15] of Integer;

// ,    
function IsSolvable(const Board: TBoard): Boolean;
var
  i, j, Inversions, EmptyRowFromBottom: Integer;
begin
  Inversions := 0;
  EmptyRowFromBottom := 0;

  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
    begin
      //       ( 1  4)
      EmptyRowFromBottom := 4 - (i div 4);
      Continue;
    end;

    //    (     )
    for j := i + 1 to 15 do
    begin
      if (Board[j] <> 0) and (Board[i] > Board[j]) then
        Inc(Inversions);
    end;
  end;

  //  '   4x4:
  //        (2  4),     .
  //    (1  3)   .
  if (EmptyRowFromBottom mod 2) = 0 then
    Result := (Inversions mod 2) = 1
  else
    Result := (Inversions mod 2) = 0;
end;

//   ' 
procedure GenerateSolvableBoard(var Board: TBoard);
var
  i, RandIdx, Temp: Integer;
begin
  repeat
    //    (0   )
    for i := 0 to 14 do
      Board[i] := i + 1;
    Board[15] := 0;

    //   Գ-
    for i := 15 downto 1 do
    begin
      RandIdx := Random(i + 1);
      Temp := Board[i];
      Board[i] := Board[RandIdx];
      Board[RandIdx] := Temp;
    end;
  until IsSolvable(Board); // ,    ' 
end;

//    4x4  
procedure PrintBoard(const Board: TBoard);
var
  i: Integer;
begin
  Writeln('+----+----+----+----+');
  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
      Write('|    ')
    else
      Write(Format('| %2d ', [Board[i]]));

    if (i + 1) mod 4 = 0 then
    begin
      Writeln('|');
      Writeln('+----+----+----+----+');
    end;
  end;
end;

function AnsiToOem(const s: string): string;
var
  i, code: Integer;
begin
  Result := '';
  for i := 1 to Length(s) do
  begin
    code := Ord(s[i]);
    case code of
      0..127: Result := Result + Chr(code);
      192..239: Result := Result + Chr(code - 64);   // ..
      240..255: Result := Result + Chr(code - 16);   // ..
      168: Result := Result + Chr(240);              // 
      184: Result := Result + Chr(241);              // 
      //   ( OEM?)
      165: Result := Result + Chr(248);  // 
      170: Result := Result + Chr(242);  // 
      175: Result := Result + Chr(244);  // 
      180: Result := Result + Chr(249);  // 
      186: Result := Result + Chr(243);  // 
      191: Result := Result + Chr(245);  // 
//      178: Result := Result + Chr(246);  // 
//      179: Result := Result + Chr(247);  // 
//   
      178: Result := Result+Chr($49);  // 
      179: Result := Result+Chr($69);  // 

    else
      Result := Result + '?';
    end;
  end;
end;


var
  Board: TBoard;
  UserChoice: string;
  Count: Integer;
begin
  Randomize; //    
  Count := 1;

  Writeln(AnsiToOem('===     "15" ==='));
  Writeln(AnsiToOem('   100% .'));
  Writeln;

  repeat
    Writeln(AnsiToOem(' #'), Count, ':');
    GenerateSolvableBoard(Board);
    PrintBoard(Board);
    Inc(Count);

    Writeln;
    Write(AnsiToOem(' [Enter]      "q"  : '));
    Readln(UserChoice);
    Writeln;
  until LowerCase(Trim(UserChoice)) = 'q';
end.
















     Delphi.     ,      (, 15  20),          variants_15.txt    ,   .
      (      ),         4.

     

pascal
program Puzzle15FileGenerator;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBoard = array[0..15] of Integer;

// ,    
function IsSolvable(const Board: TBoard): Boolean;
var
  i, j, Inversions, EmptyRowFromBottom: Integer;
begin
  Inversions := 0;
  EmptyRowFromBottom := 0;

  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
    begin
      EmptyRowFromBottom := 4 - (i div 4);
      Continue;
    end;

    for j := i + 1 to 15 do
    begin
      if (Board[j] <> 0) and (Board[i] > Board[j]) then
        Inc(Inversions);
    end;
  end;

  if (EmptyRowFromBottom mod 2) = 0 then
    Result := (Inversions mod 2) = 1
  else
    Result := (Inversions mod 2) = 0;
end;

//   ' 
procedure GenerateSolvableBoard(var Board: TBoard);
var
  i, RandIdx, Temp: Integer;
begin
  repeat
    for i := 0 to 14 do
      Board[i] := i + 1;
    Board[15] := 0;

    for i := 15 downto 1 do
    begin
      RandIdx := Random(i + 1);
      Temp := Board[i];
      Board[i] := Board[RandIdx];
      Board[RandIdx] := Temp;
    end;
  until IsSolvable(Board);
end;

//        
procedure WriteBoardToFileAndConsole(var OutFile: TextFile; const Board: TBoard; VariantNum: Integer);
var
  i: Integer;
  LineStr: string;
begin
  Writeln(OutFile, '---  #', VariantNum, ' ---');
  Writeln('---  #', VariantNum, ' ---');

  LineStr := '';
  for i := 0 to 15 do
  begin
    if Board[i] = 0 then
      LineStr := LineStr + '[  ] '
    else
      LineStr := LineStr + Format(' %2d  ', [Board[i]]);

    if (i + 1) mod 4 = 0 then
    begin
      Writeln(OutFile, LineStr);
      Writeln(LineStr);
      LineStr := '';
    end;
  end;

  Writeln(OutFile);
  Writeln;
end;

var
  Board: TBoard;
  Count, i: Integer;
  OutFile: TextFile;
  FileName: string;
begin
  Randomize;
  FileName := 'variants_15.txt';

  Writeln('===     "15"   ===');
  Write('  ? (, 10): ');
  Readln(Count);

  if Count <= 0 then
    Count := 10;

  // ³    (UTF-8   )
  AssignFile(OutFile, FileName);
  Rewrite(OutFile);
  try
    Writeln(OutFile, '²Ͳ в   "15" (Ѳ )');
    Writeln(OutFile, '=================================================');
    Writeln(OutFile);

    Writeln;
    for i := 1 to Count do
    begin
      GenerateSolvableBoard(Board);
      WriteBoardToFileAndConsole(OutFile, Board, i);
    end;

    Writeln('!  ', Count, ' .');
    Writeln('  : ', ExpandFileName(FileName));
  finally
    CloseFile(OutFile);
  end;

  Writeln;
  Write(' [Enter]  ...');
  Readln;
end.
 ,  .

    :

  :      Board := 0;        Delphi (  Board[15] := 0;).    ,     .
    : ϳ      (, 12),    .exe  '   variants_15.txt.
  :       [  ],      .    4      1216  .
 ,       (,            )  ,      (, )    .  ?








